package com.freshcity.page.common
{
	import com.freshcity.component.container.Component;
	import com.freshcity.icons.PagingIconWin;
	import com.freshcity.lib.event.DataEvent;
	import com.freshcity.lib.event.EventName;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class PagingIcon extends Component
	{
		private var back:PagingIconWin;
		private var _totalNum:int;
		private var currentNum:int = 1;
		
		public function PagingIcon(totalNum:int,parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0)
		{
			this.mouseEnabled = false;
			this.totalNum = totalNum;
			super(parent, xpos, ypos);
			
			back.preBtn.addEventListener(MouseEvent.CLICK,onPreBtnClick);
			back.nextBtn.addEventListener(MouseEvent.CLICK,onNextBtnClick);
		}
		
		protected function onPreBtnClick(event:MouseEvent):void
		{
			this.currentNum --;
			renderContent();
			this.dispatchEvent(new DataEvent(EventName.SwitchPage,"forward"));
		}
		
		protected function onNextBtnClick(event:MouseEvent):void
		{
			this.currentNum ++;
			renderContent();
			this.dispatchEvent(new DataEvent(EventName.SwitchPage,"back"));
		}
		
		override protected function addChildren():void
		{
			super.addChildren();
			back = new PagingIconWin();
			addChild(back);
			renderContent();
		}
		
		private function renderContent():void
		{
			back.pageStr.text = this.currentNum+"/"+this.totalNum;
			if(this.currentNum>=this.totalNum){
				back.nextBtn.visible = false;
			}else{
				back.nextBtn.visible = true;
			}
			
			if(this.currentNum<=1){
				back.preBtn.visible = false;
			}else{
				back.preBtn.visible = true;
			}
		}
		
		override public function destroy():void
		{
			// TODO Auto Generated method stub
			super.destroy();
		}

		public function get totalNum():int
		{
			return _totalNum;
		}

		public function set totalNum(value:int):void
		{
			_totalNum = value;
		}
		
		
	}
}